// import { storage, tokenTableName } from '@config'
import settings from '@config/index'
import cookie from 'js-cookie'

/**
 * @description 获取accessToken
 * @returns {string|ActiveX.IXMLDOMNode|Promise<any>|any|IDBRequest<any>|MediaKeyStatus|FormDataEntryValue|Function|Promise<Credential | null>}
 */
export function getAccessToken() {
  if (settings.storage) {
    if ('localStorage' === settings.storage) {
      return localStorage.getItem(settings.tokenTableName)
    } else if ('sessionStorage' === settings.storage) {
      return sessionStorage.getItem(settings.tokenTableName)
    } else if ('cookie' === settings.storage) {
      return cookie.get(settings.tokenTableName)
    } else {
      return localStorage.getItem(settings.tokenTableName)
    }
  } else {
    return localStorage.getItem(settings.tokenTableName)
  }
}

/**
 * @description 存储accessToken
 * @param accessToken
 * @returns {void|*}
 */
export function setAccessToken(accessToken) {
  if (settings.storage) {
    if ('localStorage' === settings.storage) {
      return localStorage.setItem(settings.tokenTableName, accessToken)
    } else if ('sessionStorage' === settings.storage) {
      return sessionStorage.setItem(settings.tokenTableName, accessToken)
    } else if ('cookie' === settings.storage) {
      return cookie.set(settings.tokenTableName, accessToken)
    } else {
      return localStorage.setItem(settings.tokenTableName, accessToken)
    }
  } else {
    return localStorage.setItem(settings.tokenTableName, accessToken)
  }
}

/**
 * @description 移除accessToken
 * @returns {void|Promise<void>}
 */
export function removeAccessToken() {
  if (settings.storage) {
    if ('localStorage' === settings.storage) {
      return localStorage.removeItem(settings.tokenTableName)
    } else if ('sessionStorage' === settings.storage) {
      return sessionStorage.clear()
    } else if ('cookie' === settings.storage) {
      return cookie.remove(settings.tokenTableName)
    } else {
      return localStorage.removeItem(settings.tokenTableName)
    }
  } else {
    return localStorage.removeItem(settings.tokenTableName)
  }
}
